package com.budwk.app.sqfwV2.service.baiyunqu;

import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.config.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.excelService.CompleteFlowExcelService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowExcelParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwV2.dto.dto.AuthenticRightDTO;
import com.budwk.app.sqfwV2.enums.AuthenticRightEnum;
import com.budwk.app.sqfwV2.generator.baiyunqv.AuthenticRightExcelGenerator;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.List;

@IocBean
public class AuthenticRightV2ExcelService extends CompleteFlowExcelService {


    @Override
    protected void customerPopularExcelGenerateParam(ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    public List<ExcelGeneratorConfig> getExcelGeneratorConfig(List<ExcelCreateConfig> excelCreateConfigList) {
        List<ExcelGeneratorConfig> excelGeneratorConfigs=new ArrayList<>();
        ExcelGeneratorConfig generatorConfig=new ExcelGeneratorConfig();
        String code = excelCreateConfigList.get(0).getFileIdentity();
        generatorConfig.setExcelGenerator(new AuthenticRightExcelGenerator(AuthenticRightEnum.getExcelTemplatName(Integer.parseInt(code))));
        generatorConfig.setExcelCreateConfig(excelCreateConfigList.get(0));
        excelGeneratorConfigs.add(generatorConfig);
        return excelGeneratorConfigs;
    }

    @Override
    public AbstractCustomerPdfCreateField getAbstractCustomerField(String jsonStr) {
        return JSONUtil.toBean(jsonStr,AuthenticRightDTO.class);
    }
    @Override
    protected List<ExcelRowDataParam> getExcelRowData(CompleteFlowExcelParamForController paramForController) {
        String code = paramForController.getExcelCreateConfigList().get(0).getFileIdentity();
        String fillStartDate = (String) paramForController.getCustomerParam().get("fillStartDate");
        String fillEndDate = (String) paramForController.getCustomerParam().get("fillEndDate");
        Dao dao =  NutzIocUtils.getBean(Dao.class);
        Sql sql = Sqls.create("select *\n" +
                "from\n" +
                "\t(\n" +
                "\tselect\n" +
                "\t\trow_number() over (partition by sqfw_id\n" +
                "\torder by\n" +
                "\t\tfill_date desc) as rk ,\n" +
                "\t\t*\n" +
                "\tfrom\n" +
                "\t\tshequ.sqfw_form_summary_new\n" +
                "\twhere\n" +
                "\ttype ='12345确权' and param_data->>'caseType' = '$castType' and fill_date between '$fillStartDate' and '$fillEndDate' and create_by='$userId' order by fill_date desc ) t1\n" +
                "where\n" +
                "\tt1.rk = 1");
        sql.setCallback(Sqls.callback.entities());
        sql.setVar("fillStartDate",fillStartDate)
                .setVar("fillEndDate", fillEndDate)
                .setVar("userId",SecurityUtil.getUserId())
                .setVar("castType",AuthenticRightEnum.getName(Integer.parseInt(code)));
        sql.setEntity(dao.getEntity(Sqfw_formSummary_new.class));
        dao.execute(sql);
        List<Sqfw_formSummary_new> formSummaryNewList = sql.getList(Sqfw_formSummary_new.class);
        return transformFormSummaryToExcelRowData(formSummaryNewList);
    }



}